import {ModalResult} from "./types";

Component({
    properties: {
        showModal: Boolean,
        showCancel: Boolean,
        title: String,
        contents: String
    },

    options: {
        addGlobalClass: true,
    },

    data: {
        resolve: undefined as ((r: ModalResult) => void)|undefined,
    },

    methods: {
        onCancel() {
            /* this.setData({
                 showModal: false
             })
             this.triggerEvent('cancel')*/
            this.hideModal('cancel')
        },

        onOk() {
            this.hideModal('ok')
        },

        // hideModal(res: 'ok'|'cancel'|'close') {
        hideModal(res: ModalResult) {
            this.setData({
                showModal: false
            })
            this.triggerEvent(res)
            if (this.data.resolve) {
                this.data.resolve(res)
            }
        },

        // showModal(): Promise<'ok'|'cancel'|'close'> {
        showModal(): Promise<ModalResult> {
            this.setData({
                showModal: true
            })
            return new Promise((resolve) => {
                this.data.resolve = resolve // save resolve function
            })
        }
    }
})
